package com.beerandjesus.sportofkings.handicapper.strategy.brisnet.singlefile;

import com.beerandjesus.sportofkings.handicapper.model.NonMaidenHorseAnalysisResultImpl;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.Horse;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.PastPerformance;
import org.apache.commons.lang.StringUtils;

public class WinsOffPaceResolver {
    public WinsOffPaceResolver() {
    }

    void resolveWinsOffPace(NonMaidenHorseAnalysisResultImpl result, Horse h) {
        // Note if runner can win off pace.
        // Any occurrence in past performances passes.
        Boolean winsOffPace = false;
        for (PastPerformance perf : h.getPastPerformances()) {
            if (equal(perf.getFinishPosition(), 1) && greaterThan(perf.getCallPositionSecond(), 1)) {
                winsOffPace = true;
                break;
            }
        }
        result.setWinsOffPace(winsOffPace);
    }

    private boolean equal(String value, int i) {
        if (StringUtils.isEmpty(value)) {
            return false;
        }
        try {
            return (i == new Integer(value));
        } catch (NumberFormatException e) {
            return false;
        }
    }

    private boolean greaterThan(String value, int i) {
        if (StringUtils.isEmpty(value)) {
            return false;
        }
        try {
            return (new Integer(value) > i);
        } catch (NumberFormatException e) {
            return false;
        }
    }


}